package com.bdqn.t296.noso.controller;

import com.bdqn.t296.noso.entity.Paiban;
import com.bdqn.t296.noso.service.PaibanService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("paiban")
public class PaibanController {
    @Resource
    private PaibanService paibanService;

    @RequestMapping("/paibanPage")
    public String toList(Model model) {
        List<Paiban> paiban = paibanService.likeSel("");
        model.addAttribute("paiban", paiban);
        return "paiban/paiban";
    }

    @RequestMapping("/byIdSerach")
    @ResponseBody
    public List<Paiban> toList(@RequestParam("doctorName") String doctorName) {
        List<Paiban> paiban = paibanService.likeSel(doctorName);
        return paiban;
    }

    @RequestMapping("/del")
    @ResponseBody
    public Object deleteById(String doctorId) {
        Integer doctorId1 = Integer.parseInt(doctorId);
        int i = paibanService.del(doctorId1);
        Map map =new HashMap();
        if (i > 1) {
            map.put("status", 1);
        } else {
            map.put("status", 2);
        }
        return map;
    }

    @RequestMapping("edit")
    public Object editInfo(String doctorId,Model model){
        int doId=Integer.parseInt(doctorId);
        List<Paiban> paiban = paibanService.likeSel("");
        Paiban pb=null;
        System.out.println("did:"+doctorId);
        for (Paiban paiban1 : paiban) {
            System.out.println(paiban1.getDoctorId());
            if(paiban1.getDoctorId()==doId){
                pb=paiban1;
                break;
            }
        }
        model.addAttribute("paiban",pb);
        return "paiban/edit";
    }
}
